//
//  SecondViewController.m
//  UI10_UITableViewHomework
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SecondViewController.h"
#import <AssetsLibrary/ALAsset.h>

#import <AssetsLibrary/ALAssetsLibrary.h>

#import <AssetsLibrary/ALAssetsGroup.h>

#import <AssetsLibrary/ALAssetRepresentation.h>

@interface SecondViewController ()
<
UINavigationBarDelegate,
UINavigationControllerDelegate,
UITextFieldDelegate,
UIImagePickerControllerDelegate
>

@property (nonatomic,retain) UITextField *textFieldName;
@property (nonatomic,retain) UITextField *textFieldNumber;
@property (nonatomic,retain) UITextField *textFieldQQ;
@property (nonatomic,retain) UIButton *headerButton;
@property (nonatomic,retain) UIImageView *imageView;


@end

@implementation SecondViewController

-(void)dealloc{
    [_name release];
    [_number release];
    [_qq release];
    [_header release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.name;
    
    
    
    [self createBackButton];
    [self createView];
}

-(void)createBackButton{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 24, 24);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

-(void)createView {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
    [self.view addSubview:view];
    view.center = CGPointMake(self.view.center.x, 450);
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.header]];
//    imageView.frame = CGRectMake(50, 50, 150, 150);
//    imageView.center = CGPointMake(self.view.center.x, 200);
//    [self.view addSubview:imageView];
//    [imageView release];
    
    self.headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headerButton.frame = CGRectMake(50, 50, 150, 150);
    self.headerButton.center = CGPointMake(self.view.center.x, 200);
    [self.headerButton setImage:[UIImage imageNamed:self.header] forState:UIControlStateNormal];
    [self.view addSubview:self.headerButton];
    [self.headerButton addTarget:self action:@selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    labelName.text = @"姓名:";
    [view addSubview:labelName];
    [labelName release];
    
    UILabel *labelNumber = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 50, 30)];
    labelNumber.text = @"电话:";
    [view addSubview:labelNumber];
    [labelNumber release];
    
    UILabel *labelQQ = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 50, 30)];
    labelQQ.text = @"QQ:";
    [view addSubview:labelQQ];
    [labelQQ release];
    
    self.textFieldName = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, 150, 30)];
    self.textFieldName.text = self.name;
    [view addSubview:self.textFieldName];
    self.textFieldName.layer.borderWidth = 1;
    self.textFieldName.layer.cornerRadius = 8;
    self.textFieldName.delegate = self;
    [self.textFieldName release];
    
    self.textFieldNumber = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 150, 30)];
    self.textFieldNumber.text = self.number;
    [view addSubview:self.textFieldNumber];
    self.textFieldNumber.layer.borderWidth = 1;
    self.textFieldNumber.layer.cornerRadius = 8;
    self.textFieldNumber.delegate = self;
    [self.textFieldNumber release];
    
    self.textFieldQQ = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 150, 30)];
    self.textFieldQQ.text = self.qq;
    [view addSubview:self.textFieldQQ];
    self.textFieldQQ.layer.borderWidth = 1;
    self.textFieldQQ.layer.cornerRadius = 8;
    self.textFieldQQ.delegate = self;
    [self.textFieldQQ release];
    
}

-(void)headerButtonAction:(UIButton *)headerButton{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    
    //模态推出
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}

/** 获得一张系统相册内的图片的文件名和文件 */ //暂时不适用?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//    [self.headerButton setImage:image forState:UIControlStateNormal];
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (image !=nil) {
        
        
        NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
        
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
            ALAssetRepresentation *representation = [myasset defaultRepresentation];
            NSString *fileName = [representation filename];
          //  self.header = fileName;
            NSLog(@"fileName : %@",fileName);
        };
        
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:imageURL
                       resultBlock:resultblock
                      failureBlock:nil];
        
        [self.headerButton setImage:image forState:UIControlStateNormal];
        
        [self.delegate backImage:image withIndexPath:self.indexPath];
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
}

-(void)backButtonAction:(UIButton *)backButton{
    [self.delegate backToFirstViewAndChangeCellWithName:self.textFieldName.text withNumber:self.textFieldNumber.text withQQ:self.textFieldQQ.text withHeader:self.header withIndexPath:self.indexPath];
    [self.navigationController popViewControllerAnimated:YES];
}



/** return取消键盘 */
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
